<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>每个域一种语言 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/one-lang-fields.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/one-lang-fields.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/one-lang-fields.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/one-lang-fields.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="languages.html">处理人类语言</a></span>
»
<span class="breadcrumb-link"><a href="language-intro.html">开始处理各种语言</a></span>
»
<span class="breadcrumb-node">每个域一种语言</span>
</div>
<div class="navheader">
<span class="prev">
<a href="one-lang-docs.html">« 每份文档一种语言</a>
</span>
<span class="next">
<a href="mixed-lang-fields.html">混合语言域 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="one-lang-fields"></a>每个域一种语言<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/200_Language_intro/50_One_language_per_field.asciidoc">edit</a>
</h2>
</div></div></div>
<p>对于一些实体类，例如:产品、电影、法律声明，
通常这样的一份文本会被翻译成不同语言的文档。虽然这些不同语言的文档可以单独保存在各自的索引中。但另一种更合理的方式是同一份文本的所有翻译统一保存在一个索引中。。</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
   "title":     "Fight club",
   "title_br":  "Clube de Luta",
   "title_cz":  "Klub rváčů",
   "title_en":  "Fight club",
   "title_es":  "El club de la lucha",
   ...
}</pre>
</div>
<p>每份翻译存储在不同的域中，根据域的语言决定使用相应的分析器：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">PUT /movies
{
  "mappings": {
    "movie": {
      "properties": {
        "title": { <a id="CO131-1"></a><i class="conum" data-value="1"></i>
          "type":       "string"
        },
        "title_br": { <a id="CO131-2"></a><i class="conum" data-value="2"></i>
            "type":     "string",
            "analyzer": "brazilian"
        },
        "title_cz": { <a id="CO131-3"></a><i class="conum" data-value="2"></i>
            "type":     "string",
            "analyzer": "czech"
        },
        "title_en": { <a id="CO131-4"></a><i class="conum" data-value="2"></i>
            "type":     "string",
            "analyzer": "english"
        },
        "title_es": { <a id="CO131-5"></a><i class="conum" data-value="2"></i>
            "type":     "string",
            "analyzer": "spanish"
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO131-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">title</code> 域含有title的原文，并使用 <code class="literal">standard</code> （标准）分析器。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO131-2"><i class="conum" data-value="2"></i></a><a href="#CO131-3"></a><a href="#CO131-4"></a><a href="#CO131-5"></a></p>
</td>
<td align="left" valign="top">
<p>其他字段使用适合自己语言的分析器。</p>
</td>
</tr>
</table>
</div>
<p>在维持干净的词频方面，虽然 <em>index-per-language</em> （一种语言一份索引的方法），不像 <em>field-per-language</em> （一种语言一个域的方法）分开索引那么灵活。但是使用 <a class="xref" href="mapping-intro.html#updating-a-mapping" title="更新映射"><code class="literal">update-mapping</code> API</a> 添加一个新域也很简单，那些新域需要新的自定义分析器，这些新分析器只能在索引创建时被装配。有一个变通的方案，你可以先关闭这个索引  <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-open-close.html" class="ulink" target="_top">close</a> ，然后使用 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-update-settings.html" class="ulink" target="_top"><code class="literal">update-settings</code> API</a> ，重新打开这个索引，但是关掉这个索引意味着得停止服务一段时间。</p>
<p>文档的一种语言可以单独查询，也可以通过查询多个域来查询多种语言。我们甚至可以通过对特定语言设置偏好来提高字段优先级：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">GET /movies/movie/_search
{
    "query": {
        "multi_match": {
            "query":    "club de la lucha",
            "fields": [ "title*", "title_es^2" ], <a id="CO132-1"></a><i class="conum" data-value="1"></i>
            "type":     "most_fields"
        }
    }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO132-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>这个搜索查询所有以 <code class="literal">title</code> 为前缀的域，但是对 <code class="literal">title_es</code> 域加权重 <code class="literal">2</code> 。其他的所有域是中性权重 <code class="literal">1</code> 。</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="one-lang-docs.html">« 每份文档一种语言</a>
</span>
<span class="next">
<a href="mixed-lang-fields.html">混合语言域 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>